package com.happyscan138.makefun.Fragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.happyscan138.makefun.About;
import com.happyscan138.makefun.BaseAdapter.myself;
import com.happyscan138.makefun.MyFriend;
import com.happyscan138.makefun.R;
import com.happyscan138.makefun.Urls.Urls;
import com.happyscan138.makefun.View.RoundImageView;
import com.happyscan138.makefun.adapter.myselfAdapter;
import com.happyscan138.makefun.base.http;
import com.happyscan138.makefun.more_myself;
import com.happyscan138.makefun.more_set;
import com.happyscan138.makefun.myCollection;
import com.happyscan138.makefun.myDiscuss;
import com.happyscan138.makefun.myJoin;
import com.happyscan138.makefun.myMessage;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.squareup.picasso.Picasso;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cz.msebera.android.httpclient.Header;

import static android.content.Context.MODE_PRIVATE;
import static com.happyscan138.makefun.base.http.client;
import static com.loopj.android.http.AsyncHttpClient.log;

/**
 * Created by Administrator on 2016/11/22.
 */

public class more_Fragment extends android.support.v4.app.Fragment implements View.OnClickListener {
    private ImageView imageView_myself_zhedie;
    private ListView listViewMyself;
    //定义一个动态数组
    private List<myself> listItem;
    private RoundImageView myselfHead;
    private Button set;
    private TextView about;
    private SharedPreferences preferences;
    private SharedPreferences.Editor editor;
    private String accessy_ley;
    private Context mContext;
    private TextView nick_name,sign;
    private LinearLayout PerLay;
    private String PersionalUserID;
    private Intent intent;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.morefragment,container,false);
        initView(view);
        getAdapterView();
        preferences = getActivity().getSharedPreferences("UserInfo", MODE_PRIVATE);
        accessy_ley=preferences.getString("UserId","");
        PersionalUserID=preferences.getString("persionalID","");
        client=new AsyncHttpClient();
        http.params=new RequestParams();
        http.params.put("access_key",accessy_ley);
        log.e("params", http.params.toString());
        client.post("http://120.25.149.212/wenda/cli-apiaccv1-userinfo.php", http.params,new JsonHttpResponseHandler(){
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                String jsonStr=response.toString();
                log.e("jsonStr",jsonStr);
                try {
                    JSONObject jsonObject=new JSONObject(jsonStr);
                    JSONObject data=jsonObject.getJSONObject("data");
                    String nick=data.getString("nick_name");
                    String mySelfUserId=data.getString("user_id");
                    editor=preferences.edit();
                    editor.putString("user_id",mySelfUserId);
                    editor.commit();
                    String signText=data.getString("sign");
                    String path= Urls.base+data.getString("img_path");
                    nick_name.setText(nick);
                    sign.setText(signText);
                    Picasso.with(mContext).load(path).into(myselfHead);

                } catch (JSONException e) {
                    e.printStackTrace();
                }


            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                super.onFailure(statusCode, headers, responseString, throwable);
            }
        });

        return view;
    }
    public void initView(View view){
        PerLay=(LinearLayout)view.findViewById(R.id.persion);
        PerLay.setOnClickListener(this);
        listViewMyself=(ListView) view.findViewById(R.id.listview_myself);
        imageView_myself_zhedie=(ImageView)view.findViewById(R.id.myself_zhedie);
        myselfHead=(RoundImageView)view.findViewById(R.id.myself_head);
        nick_name=(TextView) view.findViewById(R.id.myself_nickName);
        sign=(TextView)view.findViewById(R.id.myself_sign);
        set=(Button) view.findViewById(R.id.more_set);
        set.setOnClickListener(this);
        about=(TextView)view.findViewById(R.id.more_ahout);
        about.setOnClickListener(this);
        imageView_myself_zhedie.setOnClickListener(this);
        myselfHead.setOnClickListener(this);
        listViewMyself.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                Intent intent = null;
              switch (position){
                  case 0:intent=new Intent(adapterView.getContext(), MyFriend.class);break;
                  case 1:intent=new Intent(adapterView.getContext(), myMessage.class);break;
    //              case 2:intent=new Intent(adapterView.getContext(), Myrelease.class);break;
                  case 2:intent=new Intent(adapterView.getContext(), myDiscuss.class);break;
                  case 3:intent=new Intent(adapterView.getContext(), myJoin.class);break;
                  case 4:intent=new Intent(adapterView.getContext(), myCollection.class);break;
              }
                startActivity(intent);
            }
        });
    }


    public View getAdapterView(){
        View view=null;
        listItem=new ArrayList<>();
            listItem.add(new myself(
                    R.mipmap.icon,
                    "我的好友",
                    R.mipmap.zhedie
            ));
            listItem.add(new myself(
                    R.mipmap.icon,
                    "我的私信",
                    R.mipmap.zhedie
            ));
//            listItem.add(new myself(
//                    R.mipmap.icon,
//                    "我的发布",
//                    R.mipmap.zhedie
//            ));
            listItem.add(new myself(
                    R.mipmap.icon,
                    "我的讨论",
                    R.mipmap.zhedie
            ));
            listItem.add(new myself(
                    R.mipmap.icon,
                    "我的参与",
                    R.mipmap.zhedie
            ));
            listItem.add(new myself(
                    R.mipmap.icon,
                    "我的收藏",
                    R.mipmap.zhedie
            ));
        listViewMyself.setAdapter(new myselfAdapter(getActivity(),listItem));
        return view;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){

            case R.id.persion:
                intent=new Intent();
                intent.putExtra("infoUserID",PersionalUserID);
                intent.setClass(getActivity(),more_myself.class);
                startActivity(intent);
                break;
            case R.id.more_set:
                startActivity(new Intent(getActivity(),more_set.class));
                break;
            case R.id.more_ahout:
                startActivity(new Intent(getActivity(), About.class));
                break;
        }
    }
}
